import React from 'react';
import { createForm } from 'rc-form';
import { Button, InputItem, WingBlank, WhiteSpace,Toast } from 'antd-mobile';
import 'antd-mobile/dist/antd-mobile.css';
import request from '../../../../utils/request';
import { hashHistory} from 'react-router';

class Login extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            pickerValue: [],
            dpValue: null,
            username:'',
            pwd:null,
        };
    }
    componentDidMount() {
        this.props.changeTitle('登录');
    }

    // 登录
    handleOk = (e) => {
        // const { getFieldError } = this.props.form;
        let userInfo = this.props.form.getFieldsValue(); //{{userName: "111", passWord: "111"}}
        if (userInfo.userName == ''){
            Toast.info("用户名不能为空！",1);
            return;
        }
        if (userInfo.password == ''){
            Toast.info("密码不能为空！",1);
            return;
        }
        this.props.form.validateFields({ force: true },
            (err, values) => {
                if (!err) {
                    request('UserLoginService','login', {
                        method: 'POST',
                        body: values,
                    }).then(function (value) {
                        // console.log(value);
                        if (value.data.result === 0){
                            // sessionStorage.setItem("isLogin",1);
                            localStorage.setItem("isLogin",1);
                            sessionStorage.setItem("selectTab",'myTab');
                            const { userInfo } = value.data.resultData;
                            localStorage.setItem("userName",userInfo.user_name);
                            hashHistory.push({
                                pathname: '/',
                                state: userInfo
                            })
                        }else if (value.data.result === 2){
                            Toast.info("用户名或密码错误！",1);
                        }
                    });
                }
            }
        );
    }
    // 注册
    register = (e) => {
        hashHistory.push({
            pathname: '/register'
        })
    }
    // 忘记密码
    forgetPwd = (e) => {
        let user = this.props.form.getFieldsValue(); //{{userName: "111", passWord: "111"}}
        if (user.userName == ''){
            Toast.info("请输入用户名！",1);
            return;
        }else {
            request('PwdManagerService','forgetPwd', {
                method: 'POST',
                body: {userName:user.userName},
            }).then(function (value) {
                const { userInfo } = value.data.resultData;
                const question = userInfo.question;
                const answer = userInfo.answer;
                if (value.data.result === 0){
                    hashHistory.push({
                        pathname: '/user/anserComfirm',
                        state: {
                            question: question,
                            answer: answer,
                            userName:user.userName
                        },
                    })
                }else if (value.data.result === 1){
                    Toast.info("用户名不存在",1);
                }
            });
        }
    }
    render() {
        const { getFieldProps } = this.props.form;
        const { getFieldError } = this.props.form;
        return (<div >
            <WhiteSpace/>
            <InputItem
                {...getFieldProps('userName',
                    {
                        initialValue: '',
                        rules: [
                            {required: true, message: '用户名不能为空!'},
                        ],
                    })}
                error={!!getFieldError('userName')}
                onErrorClick={() => {
                    Toast.fail(getFieldError('userName'), 1);
                }}
                type="text"
                placeholder="请输入用户名">
                账号
            </InputItem>
            <InputItem
                {...getFieldProps('password',
                    {
                        initialValue: '',
                        rules: [
                            {required: true, message: '密码不能为空!'},
                        ],
                    })}
                error={!!getFieldError('password')}
                onErrorClick={() => {
                    Toast.fail(getFieldError('password'), 1);
                }}
                type="password"
                placeholder="请输入密码">
                密码
            </InputItem>
            <WingBlank>
                <WhiteSpace/>
                <Button type="primary" size="large" onClick={this.handleOk}>登 录</Button><WhiteSpace/>
                <span>
                    <span onClick={this.register} >快速注册</span>
                    <span style={{float:'right'}} onClick={this.forgetPwd}>忘记密码</span>
                </span>
            </WingBlank>
        </div>);
    }
}

export default createForm()(Login);
